/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.ai.minesweeper.util;

/**
 *
 * @author pborrego
 */
public class SVector2D {
    public double x;
    public double y;

    public SVector2D()
    {
        x = 0;
        y = 0;
    }
    
    public SVector2D(double a, double b)
    {
        x = a;
        y = b;
    }


    //we need some overloaded operators
    public void incrementBy(SVector2D rhs)
    {
        x += rhs.x;
        y += rhs.y;
    }

    public void decrementBy(SVector2D rhs)
    {
            x -= rhs.x;
            y -= rhs.y;
    }

    public void multiplyBy(double rhs)
    {
        x *= rhs;
        y *= rhs;
    }

    public void divideBy(double rhs)
    {
        x /= rhs;
        y /= rhs;
    }
        
    //overload the * operator
    public static SVector2D multiply(SVector2D lhs, double rhs)
    {
        SVector2D result = new SVector2D(lhs.x, lhs.y);
        result.multiplyBy(rhs);
        return result;
    }
    
    //overload the - operator
    public static SVector2D subtract(SVector2D lhs, SVector2D rhs)
    {
        SVector2D result = new SVector2D(lhs.x, lhs.y);
        result.x -= rhs.x;
        result.y -= rhs.y;

        return result;
    }
 
    
    //------------------------- Vec2DLength -----------------------------
    //
    //	returns the length of a 2D vector
    //--------------------------------------------------------------------
    public static double Vec2DLength(SVector2D v)
    {
        return Math.sqrt(v.x * v.x + v.y * v.y);
    }

    //------------------------- Vec2DNormalize -----------------------------
    //
    //	normalizes a 2D Vector
    //--------------------------------------------------------------------
    public static void Vec2DNormalize(SVector2D v)
    {
        double vector_length = Vec2DLength(v);

        v.x = v.x / vector_length;
        v.y = v.y / vector_length;
    }

    //------------------------- Vec2DDot --------------------------
    //
    //	calculates the dot product
    //--------------------------------------------------------------------
    public static double Vec2DDot(SVector2D v1, SVector2D v2)
    {
        return v1.x*v2.x + v1.y*v2.y;
    }

    //------------------------ Vec2DSign --------------------------------
    //
    //  returns positive if v2 is clockwise of v1, minus if anticlockwise
    //-------------------------------------------------------------------
    public static int Vec2DSign(SVector2D v1, SVector2D v2)
    {
        if (v1.y*v2.x > v1.x*v2.y)
        { 
            return 1;
        }

        return -1;
    }
}

